package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.HotProductHomeMapper;
import com.wteam.dragon.shop.mapper.ProductMapper;
import com.wteam.dragon.shop.pojo.bean.HotProductHome;
import com.wteam.dragon.shop.pojo.bean.Product;
import com.wteam.dragon.shop.service.HotProductHomeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/25 20:47
 */
@Service
@RequiredArgsConstructor
public class HotProductHomeServiceImpl implements HotProductHomeService {
    private ProductMapper productMapper;
    private HotProductHomeMapper hotProductHomeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addHotProductHome(List<Long> ids) {
        ids.forEach(id -> {
            Product product = productMapper.selectById(id);
            HotProductHome hotProductHome = new HotProductHome();
            hotProductHome.setProductId(id);
            hotProductHome.setProductName(product.getName());
            hotProductHomeMapper.insert(hotProductHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delHotProductHome(List<Long> ids) {
        hotProductHomeMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editHotProductHome(Long id, Long sort) {
        HotProductHome hotProductHome = new HotProductHome();
        hotProductHome.setId(id);
        hotProductHome.setSort(sort);
        hotProductHomeMapper.updateById(hotProductHome);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageHotProductHome(Page<HotProductHome> page, String productName, String recommendStatus) {
        QueryWrapper<HotProductHome> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(productName != null, HotProductHome::getProductName, productName)
                .eq(recommendStatus != null, HotProductHome::getRecommendStatus, recommendStatus);
        Page<HotProductHome> hotProductHomePage = hotProductHomeMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("hotProductHomePage", hotProductHomePage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editHotProductHomeStatus(List<Long> ids, Integer status) {
        ids.forEach(id -> {
            HotProductHome hotProductHome = new HotProductHome();
            hotProductHome.setId(id);
            hotProductHome.setRecommendStatus(status);
            hotProductHomeMapper.updateById(hotProductHome);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

}
